package cn.iocoder.yudao.module.reading.dal.mysql.book;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reading.controller.admin.book.vo.PictureBookPageReqVO;
import cn.iocoder.yudao.module.reading.dal.dataobject.book.PictureBookDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 绘本 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PictureBookMapper extends BaseMapperX<PictureBookDO> {

    /**
     * 根据绘本集ID查询绘本列表
     *
     * @param bookSetId 绘本集ID
     * @return 绘本列表
     */
    default List<PictureBookDO> selectByBookSetId(Long bookSetId) {
        return selectList(new LambdaQueryWrapperX<PictureBookDO>()
                .eq(PictureBookDO::getBookSetId, bookSetId)
                .eq(PictureBookDO::getIsActive, true)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

    /**
     * 根据绘本集ID列表查询绘本列表
     *
     * @param bookSetIds 绘本集ID列表
     * @return 绘本列表
     */
    default List<PictureBookDO> selectByBookSetIds(List<Long> bookSetIds) {
        return selectList(new LambdaQueryWrapperX<PictureBookDO>()
                .in(PictureBookDO::getBookSetId, bookSetIds)
                .eq(PictureBookDO::getIsActive, true)
                .orderByAsc(PictureBookDO::getBookSetId)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

    /**
     * 搜索绘本
     *
     * @param keyword 关键词
     * @return 绘本列表
     */
    default List<PictureBookDO> searchBooks(String keyword) {
        return selectList(new LambdaQueryWrapperX<PictureBookDO>()
                .like(PictureBookDO::getTitle, keyword)
                .eq(PictureBookDO::getIsActive, true)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

    /**
     * 获取最大ID
     *
     * @return 最大ID，如果表为空则返回null
     */
    @Select("SELECT MAX(id) FROM picture_books")
    Long selectMaxId();

    /**
     * 分页查询绘本
     *
     * @param pageReqVO 分页查询条件
     * @return 绘本分页结果
     */
    default PageResult<PictureBookDO> selectPage(PictureBookPageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<PictureBookDO>()
                .likeIfPresent(PictureBookDO::getTitle, pageReqVO.getTitle())
                .eqIfPresent(PictureBookDO::getBookSetId, pageReqVO.getBookSetId())
                .eqIfPresent(PictureBookDO::getIsActive, pageReqVO.getIsActive())
                .orderByAsc(PictureBookDO::getBookSetId)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

    /**
     * 根据ID列表批量查询绘本
     *
     * @param ids ID列表
     * @return 绘本列表
     */
    default List<PictureBookDO> selectBatchIds(List<Long> ids) {
        return selectList(new LambdaQueryWrapperX<PictureBookDO>()
                .in(PictureBookDO::getId, ids)
                .eq(PictureBookDO::getIsActive, true)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

    /**
     * 根据语言查询绘本列表
     *
     * @param language 语言类型
     * @return 绘本列表
     */
    @Select("SELECT pb.* FROM picture_books pb " +
            "INNER JOIN book_sets bs ON pb.book_set_id = bs.id " +
            "WHERE pb.is_active = 1 AND bs.is_active = 1 AND bs.language = #{language} " +
            "ORDER BY pb.sort_order ASC")
    List<PictureBookDO> selectByLanguage(String language);

    /**
     * 查询绘本列表
     *
     * @param bookSetId 绘本集ID
     * @param title     标题
     * @return 绘本列表
     */
    default List<PictureBookDO> selectList(Long bookSetId, String title) {
        return selectList(new LambdaQueryWrapperX<PictureBookDO>()
                .eqIfPresent(PictureBookDO::getBookSetId, bookSetId)
                .likeIfPresent(PictureBookDO::getTitle, title)
                .eq(PictureBookDO::getIsActive, true)
                .orderByAsc(PictureBookDO::getSortOrder));
    }

}
